<nz-card>
  <nz-descriptions
    nzTitle="基本信息"
    nzBordered
    nzSize="small"
    [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
  >
    <nz-descriptions-item nzTitle="单号">
      {{ detailsData.allocationNumber || '-' }}
      <i
        *ngIf="detailsData.allocationNumber"
        nz-icon
        nzType="copy"
        nzTheme="outline"
        nz-tooltip
        nzTooltipTitle="点击复制"
        (click)="coprText(detailsData.allocationNumber)"
      ></i>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建人">{{ detailsData.founder || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建时间">{{ detailsData.createTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="状态">
      <span [ngStyle]="{'color': detailsData.state | storageAllocationState: 'color'}">
        {{ detailsData.state | storageAllocationState: 'label' }}
      </span>
    </nz-descriptions-item>

    <nz-descriptions-item nzTitle="调拨类型">
      {{ filterText(allocationTypeOptions, detailsData.type, 'content') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="出库仓库">
      {{ filterText(storeOptions, detailsData.deliveryWhId, 'id') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="入库仓库" nzSpan="2">
      {{ filterText(storeOptions, detailsData.warehousingWhId, 'id') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="备注" nzSpan="4">
      {{ detailsData.remark || '-' }}
    </nz-descriptions-item>
  </nz-descriptions>
</nz-card>

<nz-card class="m-t-4">
  <p class="detail-title">调拨明细</p>

  <div class="table-wrap">
    <nz-table
      #mxTable
      nzBordered
      nzSize="small"
      [nzShowPagination]="false"
      [nzFrontPagination]="false"
      [nzData]="detailedTableData">
      <thead>
        <tr>
          <th nzAlign="center" rowspan="2" nzWidth="70px">序号</th>
          <th nzAlign="center" colspan="3">商品信息</th>
          <th nzAlign="center" colspan="4">出库信息</th>
          <th nzAlign="center" rowspan="2">入库操作</th>
        </tr>
        <tr>
          <!-- <th nzAlign="center">商品状态</th> -->
          <th nzAlign="center">商品名称</th>
          <th nzAlign="center">商品编号</th>
          <th nzAlign="center">商品69码</th>

          <th nzAlign="center">行状态</th>
          <th nzAlign="center">调出品质</th>
          <th nzAlign="center">预计出库数量</th>
          <th nzAlign="center">实际出库数量</th>
        </tr>
      </thead>
      <tbody>
        <tr
          *ngFor="let data of mxTable.data; let index = index;"
          [ngClass]="{'tr-bg-red': data?.lineState === 1}"
        >
          <!-- 序号 -->
          <td nzAlign="center">{{ index + 1 }}</td>
          <!-- 商品状态 -->
          <!-- <td nzAlign="center">
            <span [ngStyle]="{'color': data.state === 0 ? '#999999' : '#67C23A'}">
              {{ data.state === 0 ? '禁用' : (data.state === 1 ? '启用' : '-') }}
            </span>
          </td> -->
          <!-- 商品名称 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsName || '-' }}
            </span>
          </td>
          <!-- 商品编号 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsCode || '-' }}
            </span>
          </td>
          <!-- 商品69码 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsTwoCode || '-' }}
            </span>
          </td>
          
          <!-- 行状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.lineState === 0 ? '#67C23A' : '#F56C6C'}">
              {{ data.lineState === 0 ? '正常' : (data.lineState === 1 ? '作废' : '-') }}
            </span>
          </td>
          <!-- 调出品质 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.deliveryQuality !== data.warehousingQuality ? '#F56C6C' : ''}">
              {{ data.deliveryQuality === 0 ? '不良品' : (data.deliveryQuality === 1 ? '良品' : '-') }}
            </span>
          </td>
          <!-- * 预计出库数量 -->
          <td nzAlign="center">
            {{ data?.deliveryNumber || '0' }}
          </td>
          <!-- 实际出库数量 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data?.deliveryNumber !== data?.actualDeliveryNumber ? '#FF9900' : ''}">
              {{ data.actualDeliveryNumber || '0' }}
            </span>
          </td>

          <!-- 操作 -->
          <td nzAlign="center">
            <ng-container *ngIf="data.lineState === 1; else elseZfTemplate">
              -
            </ng-container>
            <ng-template #elseZfTemplate>
              <button
                *ngIf="data.type === 1"
                nz-button
                nzType="link"
                nzSize="small"
                (click)="showSninfoModal(data)">
                操作SN
                <span *ngIf="data.actualWarehousingNumber">({{ data.actualWarehousingNumber }})</span>
              </button>

              <button
                *ngIf="data.type !== 1"
                nz-button
                nzType="link"
                nzSize="small"
                (click)="showMoreLocationModal(data)">
                多库位
                <span *ngIf="data.actualWarehousingNumber">({{ data.actualWarehousingNumber }})</span>
              </button>
            </ng-template>
          </td>
        </tr>

        <tr *ngIf="detailedTableData?.length" style="background: #fafafa; font-weight: 600;">
          <td nzAlign="center" colSpan="6">合计</td>
          <!-- 预计出库数 -->
          <td nzAlign="center">{{ calcSumTotal('deliveryNumber') }}</td>
          <!-- 实际出库数 -->
          <td nzAlign="center">{{ calcSumTotal('actualDeliveryNumber') }}</td>
          <td nzAlign="center">-</td>
        </tr>
      </tbody>
    </nz-table>
  </div>
</nz-card>

<nz-card class="m-t-4">
  <div nz-row nzJustify="center">
    <button
      nz-button
      nzType="primary"
      class="m-r-10"
      [nzLoading]="subDataLoading"
      (click)="subTemporaryData()"
    >暂存</button>

    <button
      nz-button
      nzType="primary"
      class="m-r-10"
      (click)="subWarehousOver()"
    >入库完毕</button>

    <button
      nz-button
      nzType="default"
      (click)="pageBack()"
    >返回</button>
  </div>
</nz-card>

<!-- S SN信息 Modal --> 
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="1000"
  [(nzVisible)]="isSninfoVisible"
  [nzTitle]="'SN信息'"
  (nzOnCancel)="isSninfoVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div>
        <button
          nz-button
          nzType="primary"
          (click)="subSninfoData()"
        >暂存并关闭</button>

        <span class="modal-zc-tips p-l-20">
          保存单据的时候该信息才会被真正保存
        </span>
      </div>
      
      <nz-divider nzDashed></nz-divider>

      <nz-descriptions [nzColumn]="2">
        <nz-descriptions-item nzTitle="商品编号">{{ editGoodsItem?.whGoodsCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="69码">{{ editGoodsItem?.whGoodsTwoCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="商品名称">{{ editGoodsItem?.whGoodsName || '-' }}</nz-descriptions-item>
      </nz-descriptions>

      <nz-table
        #snxxTable
        nzSize="small"
        nzBordered
        [nzFrontPagination]="false"
        [nzShowPagination]="false"
        [nzData]="sninfoTableData">
        <thead>
          <tr>
            <th nzAlign="center" nzWidth="80">序号</th>
            <th nzAlign="center">SN码</th>
            <th nzAlign="center">调出品质</th>
            <th nzAlign="center">
              调入品质
              <a nz-dropdown [nzDropdownMenu]="menu" class="m-l-10">
                <i nz-icon nzType="caret-down" nzTheme="outline"></i>
              </a>
              <nz-dropdown-menu #menu="nzDropdownMenu">
                <ul nz-menu nzSelectable>
                  <li
                    *ngFor="let option of qualityOptions"
                    nz-menu-item
                    (click)="batchQualitySelect(option)"
                  >{{ option.label }}</li>
                </ul>
              </nz-dropdown-menu>
            </th>
            <th nzAlign="center">
              调入库位
              <a nz-dropdown [nzDropdownMenu]="menuTwo" class="m-l-10">
                <i nz-icon nzType="caret-down" nzTheme="outline"></i>
              </a>
              <nz-dropdown-menu #menuTwo="nzDropdownMenu">
                <ul nz-menu nzSelectable class="kjul-wrap">
                  <li
                    *ngFor="let option of locationOptions"
                    nz-menu-item
                    (click)="batchLocationSelect(option)"
                  >{{ option.name }}</li>
                </ul>
              </nz-dropdown-menu>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let data of snxxTable.data; let index = index;">
            <!-- 序号 -->
            <td nzAlign="center">{{ index + 1 }}</td>
            <!-- SN码 -->
            <td nzAlign="center">{{ data.code || '-' }}</td>
            <!-- 调出品质 -->
            <td nzAlign="center">
              {{ data.deliveryQuality === 0 ? '不良品' : (data.deliveryQuality === 1 ? '良品' : '-') }}
            </td>
            <!-- 调入品质 -->
            <td nzAlign="center">
              <nz-select
                style="width: 200px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择品质"
                [(ngModel)]="data.warehousingQuality">
                <nz-option nzLabel="不良品" [nzValue]="0"></nz-option>
                <nz-option nzLabel="良品" [nzValue]="1"></nz-option>
              </nz-select>
            </td>
            <!-- 调入库位 -->
            <td nzAlign="center">
              <nz-select
                style="width: 200px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择库位"
                [(ngModel)]="data.warehousingLibraryId">
                <nz-option
                  *ngFor="let option of locationOptions"
                  [nzLabel]="option.name"
                  [nzValue]="option.id"
                ></nz-option>
              </nz-select>
            </td>
          </tr>
        </tbody>
      </nz-table>
    </div>
  </div>
</nz-modal>
<!-- E SN信息 Modal -->

<!-- S 多库位 Modal --> 
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [nzWidth]="1000"
  [(nzVisible)]="isMoreLocationVisible"
  [nzTitle]="'多库位'"
  (nzOnCancel)="isMoreLocationVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <div>
        <button
          nz-button
          nzType="primary"
          (click)="submitSetLocation()"
        >暂存并关闭</button>

        <span class="modal-zc-tips p-l-20">
          保存单据的时候该信息才会被真正保存
        </span>
      </div>
      
      <nz-divider nzDashed></nz-divider>

      <nz-descriptions [nzColumn]="2">
        <nz-descriptions-item nzTitle="商品编号">{{ editGoodsItem?.whGoodsCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="69码">{{ editGoodsItem?.whGoodsTwoCode || '-' }}</nz-descriptions-item>
        <nz-descriptions-item nzTitle="商品名称">{{ editGoodsItem?.whGoodsName || '-' }}</nz-descriptions-item>
      </nz-descriptions>

      <nz-table
        #dkwTable
        nzSize="small"
        nzBordered
        [nzFooter]="addLineTmp"
        [nzFrontPagination]="false"
        [nzShowPagination]="false"
        [nzData]="setLocationData">
        <thead>
          <tr>
            <th nzAlign="center" nzWidth="80">序号</th>
            <th nzAlign="center">
              调入品质
              <a
                *ngIf="setLocationData.length"
                nz-dropdown
                [nzDropdownMenu]="menuThree"
                class="m-l-10">
                <i nz-icon nzType="caret-down" nzTheme="outline"></i>
              </a>
              <nz-dropdown-menu #menuThree="nzDropdownMenu">
                <ul nz-menu nzSelectable>
                  <ng-container *ngFor="let option of qualityOptions">
                    <li
                      [hidden]="option.value === -1"
                      nz-menu-item
                      (click)="batchLocationQuality(option)">
                      {{ option.label }}
                    </li>
                  </ng-container>
                </ul>
              </nz-dropdown-menu>
            </th>
            <th nzAlign="center">库位</th>
            <th nzAlign="center">入库数量</th>
            <th nzAlign="center">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let data of dkwTable.data; let index = index;">
            <td nzAlign="center">{{ index + 1 }}</td>
            <td nzAlign="center">
              <nz-select
                style="width: 200px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择品质"
                [(ngModel)]="data.warehousingQuality">
                <nz-option nzLabel="不良品" [nzValue]="0"></nz-option>
                <nz-option nzLabel="良品" [nzValue]="1"></nz-option>
              </nz-select>
            </td>
            <td nzAlign="center">
              <nz-select
                style="width: 200px;"
                nzShowSearch
                nzAllowClear
                nzPlaceHolder="请选择库位"
                [(ngModel)]="data.warehousingLibraryId"
                (ngModelChange)="locationChange($event, index, setLocationData)">
                <nz-option
                  *ngFor="let option of locationOptions"
                  [nzLabel]="option.name"
                  [nzValue]="option.id"
                ></nz-option>
              </nz-select>
            </td>
            <td nzAlign="center">
              <nz-input-number
                style="width: 200px;"
                nzId="{{ 'numInput' + index }}"
                [nzMin]="1"
                [nzMax]="99999"
                [nzPrecision]="0"
                nzPlaceHolder="请输入入库数量"
                [(ngModel)]="data.num"
                (keydown)="snCodeKeDowm($event, index, 'numInput')"
              ></nz-input-number>
            </td>
            <td nzAlign="center">
              <button
                nz-button
                nzType="link"
                nzSize="small"
                nzDanger
                (click)="deleteKwItem(index)">
                <i nz-icon nzType="delete" nzTheme="outline"></i>
              </button>
            </td>
          </tr>
        </tbody>
      </nz-table>

      <ng-template #addLineTmp>
        <div nz-row nzJustify="center">
          <button
            nz-button
            nzType="link"
            nzSize="small"
            (click)="addLocationLineItem()">
            <i nz-icon nzType="plus" nzTheme="outline"></i>
            添加行
          </button>
        </div>
      </ng-template>
    </div>
  </div>
</nz-modal>
<!-- E 多库位 Modal -->